package com.dxp.plugins;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.service.CacheManager;
import com.apache.cache.service.impl.LoadCacheFactory;
import com.apache.cache.util.Validator;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import javax.swing.plaf.PanelUI;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname IntSignSynCachePluginImpl
 * @Description 接口和签名同步缓存
 * @Date 2020/4/16 18:46
 * @Created by fpc
 */
public class IntSignSynCachePluginImpl implements CustomMethodPlugin {
    private Logger log = Logger.getLogger(getClass());
    @Override
    public Object beforeInvoke(Map<String, Object> param) {
        String modelTypes = String.valueOf(param.get("modelTypes"));
        if(modelTypes.startsWith("d_dxInterfaceReg")){
            param.put("modelTypes","d_dxInterfaceReg,d_dxMsgFormatSet");
            String infoId = String.valueOf(param.get("dxInterfaceReg.w_infoId"));
            param.put("dxMsgFormatSet.w_interfaceId",infoId);
        }
        param.put("afterMethodKey", "intSignSynCachePlugin");
        return null;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> param) {
        log.info("fpc--afterInvoke-->>>"+param);
        CacheManager intsign = LoadCacheFactory.getInstance().getCacheManager("intsign");
        String infoId = "";
        String objName="";
        Map<String,Object> whereMap = new HashMap<>();
        String modelTypes = String.valueOf(param.get("modelTypes"));
        if(Validator.isNotNull(modelTypes)){
            String[] modelTypesArr = modelTypes.split("_");
            objName = modelTypesArr[1];
            if(modelTypes.startsWith("i_")){
                Map<String,String> idMap = (Map<String,String>)param.get("resultIds");
                infoId = idMap.get(objName+"_infoId");
            }else {
                infoId = String.valueOf(param.get(objName+".w_infoId"));
            }
        }
        if(Validator.isNotNull(infoId)){
            if(modelTypes.startsWith("d_")){
                if("dxIntSignConf".equals(objName)){//如果是签名认证配置表缓存key签名编号signCode
                    infoId=String.valueOf(param.get("dxIntSignConf.signCode"));
                }
                intsign.removeCacheObject(infoId);
            }else{
                whereMap.put(objName+".w_infoId",infoId);
                ResultEntity entity = IusPluginUtil.getInstance().select("obj", "s_"+objName, whereMap);
                if(!Validator.isEmpty(entity.getEntity())){
                    if("dxIntSignConf".equals(objName)){//如果是签名认证配置表使用签名编号做key
                        DataMap data = (DataMap)entity.getEntity();
                        infoId=String.valueOf(data.get("signCode"));
                    }
                    intsign.createCacheObject(infoId, JSONObject.fromObject(entity.getEntity()).toString());
                }
            }
        }
        return null;
    }
}
